#include <stdio.h>
#include <stdlib.h>

#include "SDL/SDL.h"
#include <string>
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"

#undef main

//窗口属性
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//将要使用的表面
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *buttonSheet = NULL;

//将要用到的事件结构体
SDL_Event event;

//将要用到的字体
TTF_Font *font = NULL;

//字体的颜色
SDL_Color textColor = { 255, 255, 255 };

SDL_Surface *load_image( std::string filename )
{
    //临时的空间，用于存储刚刚加载好的图像
    SDL_Surface* loadedImage = NULL;

    //优化后的图像，实际使用的是这个图像
    SDL_Surface* optimizedImage = NULL;

    //加载图像
    loadedImage = IMG_Load(filename.c_str() );

    //如果加载图片没有出错
    if( loadedImage != NULL )
    {
        //创建一个优化了的图像
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //释放临时的图像
        SDL_FreeSurface( loadedImage );
    }

    //如果图像优化成功
//    if( optimizedImage != NULL )
//    {
//        //映射关键色
//        Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );

//        //将所有颜色为（R 0, G 0xFF, B 0xFF）的像素设为透明。
//        SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
//    }

    //返回优化后的表面
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //新建一个临时的矩形来保存偏移量
    SDL_Rect offset;

    //将传入的偏移量保存到矩形中
    offset.x = x;
    offset.y = y;
    //执行表面的Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool load_files()
{
    //加载图像
    image = load_image( "../lesson09/background.png" );
    buttonSheet = load_image( "../lesson09/button.png" );

    //打开字体
    font = TTF_OpenFont( "../lesson09/Garuda.ttf", 28 );

    //如果加载出错
    if( image == NULL )
    {
        return false;
    }

    if( buttonSheet == NULL )
    {
        return false;
    }

    //如果字体加载出错
    if( font == NULL )
    {
        return false;
    }

    //如果所有图片加载正常
    return true;
}

void clean_up()
{
    //释放图像
    SDL_FreeSurface( image );

    //退出SDL
    SDL_Quit();
}

bool init()
{
    //初始化SDL的所有子系统
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //设置窗口
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //如果设置出错
    if( screen == NULL )
    {
        return false;
    }

    //初始化SDL_ttf
    if( TTF_Init() == -1 )
    {
        return false;
    }

    //设置窗口标题
    SDL_WM_SetCaption( "鼠标事件", NULL );

    //如果所有初始化操作都成功
    return true;
}


//按钮类
class Button
{
private:
    //按钮的属性
    SDL_Rect box;

    //精灵图中显示出来的子图
    SDL_Rect* clip;

    bool pressed;

public:
    //初始化变量
    Button( int x, int y, int w, int h );

    //处理事件并设置按钮的精灵图子图
    void handle_events();

    //将按钮显示在窗口中
    void show();
};

SDL_Rect clips[4];

enum ButtonState {
    CLIP_MOUSEOVER = 0,
    CLIP_MOUSEOUT,
    CLIP_MOUSEDOWN,
    CLIP_MOUSEUP
};

Button::Button( int x, int y, int w, int h ) {
    //设置按钮的属性
    box.x = x;
    box.y = y;
    box.w = w;
    box.h = h;

    //设置默认的子图
    clip = &clips[ CLIP_MOUSEOUT ];

    pressed = false;
}

void Button::handle_events()
{
    //鼠标坐标
    int x = 0, y = 0;

    //如果鼠标发生了移动
    if( event.type == SDL_MOUSEMOTION )
    {
        //获得鼠标坐标
        x = event.motion.x;
        y = event.motion.y;

        //如果鼠标在按钮上方
        if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
        {
            //设置按钮子图
            if(pressed == false)
                clip = &clips[ CLIP_MOUSEOVER ];
        }
        //如果不是
        else
        {
            //设置按钮子图
            clip = &clips[ CLIP_MOUSEOUT ];
            pressed = false;
        }
    }
    //如果一个鼠标按钮被按下了
    if( event.type == SDL_MOUSEBUTTONDOWN )
    {
        //如果鼠标左键被按下了
        if( event.button.button == SDL_BUTTON_LEFT )
        {
            //获取鼠标坐标
            x = event.button.x;
            y = event.button.y;

            //如果鼠标在按钮上方
            if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
            {
                //设置按钮子图
                clip = &clips[ CLIP_MOUSEDOWN ];
                pressed = true;
            }
        }
    }
    //如果一个鼠标按钮被释放了
    if( event.type == SDL_MOUSEBUTTONUP )
    {
        //如果鼠标左键被释放了
        if( event.button.button == SDL_BUTTON_LEFT )
        {
            //获取鼠标坐标
            x = event.button.x;
            y = event.button.y;

            //如果鼠标在按钮上方
            if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
            {
                //设置按钮子图
                if(pressed)
                    clip = &clips[ CLIP_MOUSEUP ];
                pressed = false;
            }
        }
    }
}

void Button::show() {
    //显示按钮
    apply_surface( box.x, box.y, buttonSheet, screen, clip );
}

void set_clips()
{
    //分割精灵图
    clips[ CLIP_MOUSEOVER ].x = 0;
    clips[ CLIP_MOUSEOVER ].y = 0;
    clips[ CLIP_MOUSEOVER ].w = 320;
    clips[ CLIP_MOUSEOVER ].h = 240;

    clips[ CLIP_MOUSEOUT ].x = 320;
    clips[ CLIP_MOUSEOUT ].y = 0;
    clips[ CLIP_MOUSEOUT ].w = 320;
    clips[ CLIP_MOUSEOUT ].h = 240;

    clips[ CLIP_MOUSEDOWN ].x = 0;
    clips[ CLIP_MOUSEDOWN ].y = 240;
    clips[ CLIP_MOUSEDOWN ].w = 320;
    clips[ CLIP_MOUSEDOWN ].h = 240;

    clips[ CLIP_MOUSEUP ].x = 320;
    clips[ CLIP_MOUSEUP ].y = 240;
    clips[ CLIP_MOUSEUP ].w = 320;
    clips[ CLIP_MOUSEUP ].h = 240;
}


int main(int argc, char *argv[])
{
    printf("hello\n");

    //确保程序一直等待quit
    bool quit = false;

    //初始化
    if( init() == false )
    {
        return 1;
    }

    //加载文件
    if( load_files() == false )
    {
        return 1;
    }

    set_clips();

    Button myButton( 170, 120, 320, 240 );

    //用白色填充窗口
    SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

    //当用户还不想退出时
    while( quit == false )
    {
        //当有事件发生时，我们需要处理它们
        while( SDL_PollEvent( &event ) )
        {
            myButton.handle_events();

            //如果用户点击了窗口右上角的关闭按钮
            if( event.type == SDL_QUIT )
            {
                //退出程序
                quit = true;
            }
        }

        //将image表面应用到窗口上
        apply_surface( 0, 0, image, screen);

        myButton.show();

        //更新窗口
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }
        SDL_Delay(10);
    }

    //清理表面并退出SDL
    clean_up();

    return 0;
}
